package com.xunge.controller;

import com.xunge.anno.Log;
import com.xunge.pojo.Attendance;
import com.xunge.pojo.AttendanceQueryParam;
import com.xunge.pojo.PageResult;
import com.xunge.pojo.Result;
import com.xunge.service.AttendanceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RequestMapping("/attendance")
@RestController
public class AttendanceController {
    @Autowired
    private AttendanceService attendanceService;

    /**
     * 分页查询考勤信息
     */
    @GetMapping
    public Result page(AttendanceQueryParam attendanceQueryParam){
        log.info("分页请求参数:{}" ,attendanceQueryParam);
        PageResult<Attendance> pageResult = attendanceService.page(attendanceQueryParam);
        return Result.success(pageResult);
    }

    /**
     * 保存考勤记录
     */
    @Log
    @PostMapping
    public Result save(@RequestBody Attendance attendance){
        log.info("保存信息：{}",attendance);
        attendanceService.save(attendance);
        return Result.success();
    }

    /**
     * 批量删除考勤记录
     */
    @Log
    @DeleteMapping
    public Result delete(@RequestParam String ids){
        log.info("根据ID批量删除考勤记录: {}",ids);
            // 将逗号分隔的字符串转换为List<Integer>
            List<Integer> idList = Arrays.stream(ids.split(","))
                    .map(String::trim)
                    .map(Integer::valueOf)
                    .collect(Collectors.toList());
            log.info("转换后的ID列表: {}", idList);
            attendanceService.delete(idList);
            return Result.success();
    }

    /**
     * 根据ID查询考勤记录
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id){
        log.info("根据ID查询考勤记录: {}",id);
        Attendance attendance = attendanceService.getById(id);
        return Result.success(attendance);
    }

    /**
     * 根据ID修改考勤记录
     */
    @Log
    @PutMapping
    public Result update(@RequestBody Attendance attendance){
        log.info("根据ID修改考勤记录：{}",attendance);
        attendanceService.update(attendance);
        return Result.success();
    }

    /**
     * 根据学生ID查询考勤记录
     */
    @GetMapping("/student/{studentId}")
    public Result getByStudentId(@PathVariable Integer studentId){
        log.info("根据学生ID查询考勤记录: {}",studentId);
        List<Attendance> attendances = attendanceService.getByStudentId(studentId);
        return Result.success(attendances);
    }

    /**
     * 根据课程ID查询考勤记录
     */
    @GetMapping("/course/{courseId}")
    public Result getByCourseId(@PathVariable Integer courseId){
        log.info("根据课程ID查询考勤记录: {}",courseId);
        List<Attendance> attendances = attendanceService.getByCourseId(courseId);
        return Result.success(attendances);
    }

    /**
     * 根据班级ID查询考勤记录
     */
    @GetMapping("/clazz/{clazzId}")
    public Result getByClazzId(@PathVariable Integer clazzId){
        log.info("根据班级ID查询考勤记录: {}",clazzId);
        List<Attendance> attendances = attendanceService.getByClazzId(clazzId);
        return Result.success(attendances);
    }
} 